﻿Public Class Creature
    'The coord of tile in DungeonMap
    Dim xTile As Integer
    Dim yTile As Integer

    'Offset from tile
    Dim xTileOffset As Integer = 0
    Dim yTileOffset As Integer = 0

    Dim texture As Bitmap
    Dim width As Integer
    Dim height As Integer

    'Used for collision detection
    Dim tilesHigh As Integer
    Dim heightTileOffset As Integer
    Dim tilesWide As Integer
    Dim widthTileOffset As Integer

    Dim tileSize As Integer = mainGame.tileSize
    Dim lastDirection As Integer = 4
    Dim movesSinceChange As Integer

    Sub New(x As Integer, y As Integer, ByRef bmp As Bitmap)
        xTile = x
        yTile = y
        texture = bmp
        width = texture.Width
        height = texture.Height
        tilesHigh = height / tileSize
        heightTileOffset = height Mod tileSize
        tilesWide = width / tileSize
        widthTileOffset = width Mod tileSize
    End Sub

    Public Function getTexture() As Bitmap
        Return texture
    End Function

    Public Sub moveRandom(ByRef map As DungeonMap)
        Dim random As New Random()
        Dim direction = lastDirection

        If movesSinceChange > 10 Then
            direction = random.Next(4)
        End If

        move(map, direction)
    End Sub

    Public Sub move(ByRef map As DungeonMap, direction As Integer)
        If direction = lastDirection Then
            movesSinceChange += 1
        Else
            movesSinceChange = 0
        End If
        lastDirection = direction

        Select Case direction
            Case 0 'Move Up
                If map.collides(xTile, yTile - 1) Then
                    'If yTileOffset < tileSize Then
                    '    yTileOffset -= 1
                    'End If
                Else
                    If width > tileSize Then
                        Dim boolCollides As Boolean = False
                        For i As Integer = 0 To tilesHigh
                            If map.collides(xTile + i, yTile) Then
                                boolCollides = True
                            End If
                        Next

                        If Not boolCollides Then
                            If yTileOffset = tileSize Then
                                yTile -= 1
                                yTileOffset = 0
                            Else
                                yTileOffset -= 1
                            End If
                        End If
                    Else
                        If yTileOffset = 0 Then
                            yTile -= 1
                            yTileOffset = tileSize
                        Else
                            yTileOffset -= 1
                        End If
                    End If
                End If

            Case 1 'Move Down
                If map.collides(xTile, yTile + (tilesHigh + 1)) Then
                    If heightTileOffset + yTileOffset < tileSize Then
                        yTileOffset += 1
                    End If
                Else
                    If width > tileSize Then
                        Dim boolCollides As Boolean = False
                        For i As Integer = 0 To tilesHigh
                            If map.collides(xTile + i, yTile + (tilesHigh + 1)) Then
                                boolCollides = True
                            End If
                        Next

                        If Not boolCollides Then
                            If yTileOffset = tileSize Then
                                yTile += 1
                                yTileOffset = 0
                            Else
                                yTileOffset += 1
                            End If
                        End If
                    Else
                        If yTileOffset = tileSize Then
                            yTile += 1
                            yTileOffset = 0
                        Else
                            yTileOffset += 1
                        End If
                    End If
                End If

            Case 2 'Move Left
                If map.collides(xTile - 1, yTile) Then
                    If xTileOffset < tileSize Then
                        xTileOffset -= 1
                    End If
                Else
                    If height > tileSize Then
                        Dim boolCollides As Boolean = False
                        For i As Integer = 0 To tilesHigh
                            If map.collides(xTile - 1, yTile + i) Then
                                boolCollides = True
                            End If
                        Next

                        If Not boolCollides Then
                            If xTileOffset = 0 Then
                                xTile -= 1
                                xTileOffset = tileSize
                            Else
                                xTileOffset -= 1
                            End If
                        End If
                    Else
                        If xTileOffset = 0 Then
                            xTile -= 1
                            xTileOffset = tileSize
                        Else
                            xTileOffset -= 1
                        End If
                    End If
                End If

            Case 3 'Move Right
                If map.collides(xTile + (tilesWide + 1), yTile) Then
                    If xTileOffset + heightTileOffset < tileSize Then
                        xTileOffset += 1
                    End If
                Else
                    If width > tileSize Then
                        Dim boolCollides As Boolean = False
                        For i As Integer = 0 To tilesWide
                            If map.collides(xTile + (tilesWide + 1), yTile + i) Then
                                boolCollides = True
                            End If
                        Next

                        If Not boolCollides Then
                            If xTileOffset = tileSize Then
                                xTile += 1
                                xTileOffset = 0
                            Else
                                xTileOffset += 1
                            End If
                        End If
                    Else
                        If xTileOffset = tileSize Then
                            xTile += 1
                            xTileOffset = 0
                        Else
                            xTileOffset += 1
                        End If
                    End If
                End If
        End Select
    End Sub

    Function getXCoord() As Integer
        Return xTile
    End Function

    Function getYCoord() As Integer
        Return yTile
    End Function

    Function getXOffset() As Integer
        Return xTileOffset
    End Function

    Function getYOffset() As Integer
        Return yTileOffset
    End Function

    Function getWidth() As Integer
        Return width
    End Function

    Function getHeight() As Integer
        Return height
    End Function
End Class
